﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KashFlowLayer
{
    public class KashFlowManager
    {
        public static int FindCostumerByEmail(string CostumerEmail)
        {
            string kashFlowUserName = ConfigurationManager.AppSettings["KashFlowUserName"];
            string kashFlowPassword = ConfigurationManager.AppSettings["KashFlowPassword"];

            KashFlowLayer.com.securedwebapp.KashFlowAPI kash = new com.securedwebapp.KashFlowAPI();
            string status, statusDetail;
            KashFlowLayer.com.securedwebapp.Customer customer = kash.GetCustomerByEmail(kashFlowUserName, kashFlowPassword, CostumerEmail, out status, out statusDetail);
            if (customer != null && customer.CustomerID != 0)
                return customer.CustomerID;
            else
                return -1;
        }

        public static int CreateCostumer(string ClientName, string ClientEmail)
        {
            string kashFlowUserName = ConfigurationManager.AppSettings["KashFlowUserName"];
            string kashFlowPassword = ConfigurationManager.AppSettings["KashFlowPassword"];

            KashFlowLayer.com.securedwebapp.KashFlowAPI kash = new com.securedwebapp.KashFlowAPI();
            KashFlowLayer.com.securedwebapp.Customer costumer = new com.securedwebapp.Customer()
            {
                Name = ClientName,
                Email = ClientEmail,
            };
            string status, statusDetail;
            return kash.InsertCustomer(kashFlowUserName, kashFlowPassword, costumer, out status, out statusDetail);
        }

        public static CommonClasses.Invoice CreateInvoice(int CustomerID, string ClientNameOnInvoice, string ClientDocumentID, string FlightID, decimal SeatPrice)
        {
            string kashFlowUserName = ConfigurationManager.AppSettings["KashFlowUserName"];
            string kashFlowPassword = ConfigurationManager.AppSettings["KashFlowPassword"];

            KashFlowLayer.com.securedwebapp.KashFlowAPI kash = new com.securedwebapp.KashFlowAPI();
            KashFlowLayer.com.securedwebapp.InvoiceLine invLine = new com.securedwebapp.InvoiceLine()
            {
                Description = "Flight ID: " + FlightID,
                Quantity = 1,
                Rate = SeatPrice,
            };
            KashFlowLayer.com.securedwebapp.InvoiceLine invLine2 = new com.securedwebapp.InvoiceLine()
            {
                Description = "Name for seat reservation: " + ClientNameOnInvoice,
            };
            KashFlowLayer.com.securedwebapp.InvoiceLine invLine3 = new com.securedwebapp.InvoiceLine()
            {
                Description = "Document ID: " + ClientDocumentID,
            };

            KashFlowLayer.com.securedwebapp.Invoice invoice = new com.securedwebapp.Invoice()
            {
                CustomerID = CustomerID,
                InvoiceDate = DateTime.Now,
                CurrencyCode = "€",
                Lines = new object[] { invLine, invLine2, invLine3 },
            };

            string status, statusDetail;

            int invoiceID = kash.InsertInvoice(kashFlowUserName, kashFlowPassword, invoice, out status, out statusDetail);
            invoice = kash.GetInvoice(kashFlowUserName, kashFlowPassword, invoiceID, out status, out statusDetail);
            CommonClasses.Invoice invInfo = new CommonClasses.Invoice() 
            {
                InvoiceID = invoice.InvoiceDBID,
                InvoiceLink = invoice.PermaLink,
            };
            return invInfo;
        }
    }
}
